package com.example.config;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class ChatConfig {
    public  static final String DEFAULT_SYSTEM = "你是热心可爱的AI助手，名字叫小团团";
    private final GlobalMemory globalMemory;
    private final CourseTools courseTools;
    @Bean
    public ChatClient deepseekChatClient(OllamaChatModel ollamaChatModel) {
        return ChatClient
                .builder(ollamaChatModel)
                .defaultSystem(DEFAULT_SYSTEM)
                .defaultAdvisors(SimpleLoggerAdvisor.builder().build())
                //.defaultAdvisors(MessageChatMemoryAdvisor.builder(globalMemory).build())
                .build();
    }
    @Bean
    public ChatClient openaiChatClient(OpenAiChatModel openAiChatModel) {
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem(DEFAULT_SYSTEM)
                .defaultAdvisors(SimpleLoggerAdvisor.builder().build())
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(globalMemory).build())
                .build();
    }
    @Bean
    public ChatClient gameClient(OpenAiChatModel openAiChatModel) {
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(SimpleLoggerAdvisor.builder().build())
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(globalMemory).build())
                .build();
    }
    @Bean
    public ChatClient serviceClient(OpenAiChatModel openAiChatModel){
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem(SystemConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(SimpleLoggerAdvisor.builder().build())
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(globalMemory).build())
                .defaultTools(courseTools)
                .build();
    }
    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel openAiEmbeddingModel) {
        return SimpleVectorStore.builder(openAiEmbeddingModel).build();
    }
    @Bean
    public ChatClient pdfChatClient(OpenAiChatModel openAiChatModel,VectorStore vectorStore){
        return ChatClient
                .builder(openAiChatModel)
                .defaultAdvisors(SimpleLoggerAdvisor.builder().build())
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(globalMemory).build())
                .defaultAdvisors(
                        QuestionAnswerAdvisor
                                .builder(vectorStore)
                                .searchRequest(
                                        SearchRequest
                                                .builder()  // 向量索引的请求参数
                                                .similarityThreshold(0.5d) // 相似度阈值
                                                .topK(2) // 返回的向量数量
                                                .build())
                                .build())
                .build();
    }
}
